package com.lzb.musicplayer.view;

import android.media.audiofx.AudioEffect;
import android.media.audiofx.EnvironmentalReverb;
import android.media.audiofx.PresetReverb;
import android.util.Log;

public class ReverbManager {
    private static final String TAG = "ReverbManager";
    
    private EnvironmentalReverb mEnvironmentalReverb;
    private PresetReverb mPresetReverb;
    private int mAudioSessionId;

    public ReverbManager(int audioSessionId) {
        mAudioSessionId = audioSessionId;
    }

    public void setReverbType(ReverbType type) {
        releaseEffects();  // 先释放之前的效果

        try {
            switch (type) {
                case NONE:
                    // 不创建效果器即为关闭
                    break;
                    
                case SMALL_ROOM:
                case MEDIUM_ROOM:
                case LARGE_ROOM:
                case MEDIUM_HALL:
                case LARGE_HALL:
                    setupEnvironmentalReverb(type);
                    break;
                    
                case PLATE:
                    setupPresetReverb();
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, "Error setting reverb: " + e.getMessage());
        }
    }

    private void setupEnvironmentalReverb(ReverbType type) {
        mEnvironmentalReverb = new EnvironmentalReverb(0, mAudioSessionId);
        // 打印设备支持的效果器
        AudioEffect.Descriptor[] effects = AudioEffect.queryEffects();
        for (AudioEffect.Descriptor d : effects) {
            Log.i("AudioEffect", "Supported effect: " + d.name + "/" + d.implementor);
        }
        // 公共参数设置
        mEnvironmentalReverb.setRoomHFLevel((short) -1000); // 高频衰减
        mEnvironmentalReverb.setDecayHFRatio((short) 0.5f);  // 衰减频率比率
        mEnvironmentalReverb.setReflectionsLevel((short) -600); // 反射电平
        mEnvironmentalReverb.setReverbLevel((short) -600);      // 混响电平
        
        // 根据类型调整参数
        switch (type) {
            case SMALL_ROOM:
                mEnvironmentalReverb.setRoomLevel((short) -1000);
                mEnvironmentalReverb.setDecayTime(300);
                mEnvironmentalReverb.setDiffusion((short) 300);
                break;
                
            case MEDIUM_ROOM:
                mEnvironmentalReverb.setRoomLevel((short) -500);
                mEnvironmentalReverb.setDecayTime(1500);
                mEnvironmentalReverb.setDiffusion((short) 500);
                break;
                
            case LARGE_ROOM:
                mEnvironmentalReverb.setRoomLevel((short) -500);
                mEnvironmentalReverb.setDecayTime(2500);
                mEnvironmentalReverb.setDiffusion((short) 700);
                break;
                
            case MEDIUM_HALL:
                mEnvironmentalReverb.setRoomLevel((short) -100);
                mEnvironmentalReverb.setDecayTime(4000);
                mEnvironmentalReverb.setDiffusion((short) 800);
                break;
                
            case LARGE_HALL:
                mEnvironmentalReverb.setRoomLevel((short)0);
                mEnvironmentalReverb.setReverbLevel((short)0);
                mEnvironmentalReverb.setDecayTime(8000);
                mEnvironmentalReverb.setDiffusion((short) 1000);
                break;
        }
        
        mEnvironmentalReverb.setEnabled(true);
    }

    private void setupPresetReverb() {
        mPresetReverb = new PresetReverb(0, mAudioSessionId);
        mPresetReverb.setPreset(PresetReverb.PRESET_PLATE);
        mPresetReverb.setEnabled(true);
    }

    private void releaseEffects() {
        if (mEnvironmentalReverb != null) {
            mEnvironmentalReverb.release();
            mEnvironmentalReverb = null;
        }
        if (mPresetReverb != null) {
            mPresetReverb.release();
            mPresetReverb = null;
        }
    }

    public enum ReverbType {
        NONE,
        SMALL_ROOM,
        MEDIUM_ROOM,
        LARGE_ROOM,
        MEDIUM_HALL,
        LARGE_HALL,
        PLATE
    }
}